package com.barunlab.ems.server.sms.control;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import org.apache.log4j.Logger;

import com.barunlab.ems.constants.EmsRequestType;
import com.barunlab.ems.model.ClientInfo;
import com.barunlab.ems.model.EmsRequest;
import com.barunlab.ems.model.EmsResponse;
import com.barunlab.ems.rmi.ServerInterface;
import com.barunlab.ems.server.sms.user.UserService;

public class SMSReceiver extends UnicastRemoteObject implements ServerInterface {
	private static final long serialVersionUID = 3217309265271970985L;
	
	private static Logger LOGGER = Logger.getLogger(SMSReceiver.class);
	
	private static SMSReceiver smsReceiver;
	
	protected SMSReceiver() throws RemoteException {
		super();
	}
	
	public static SMSReceiver getInstance() throws RemoteException {
		if ( smsReceiver == null ) {
			smsReceiver = new SMSReceiver();
		}
		
		return smsReceiver;
	}
	
	@Override
	public EmsResponse command(EmsRequest request) throws RemoteException {
		LOGGER.debug("command");
		
		ClientInfo clientInfo = request.getClientInfo();
		EmsRequestType requestType = request.getRequestType();

		if ( clientInfo != null ) {
			LOGGER.debug(String.format("clientInfo(name=%s,ip=%s,level=%s) request:%s", 
					clientInfo.getName(),
					clientInfo.getIp(),
					clientInfo.getLevel(),
					request.getRequestType()));
		}

		EmsResponse response = null;
		switch ( requestType ) {
		case LOGIN :
			response = UserService.login(request);
			break;
		case LOGOUT :
			response = UserService.logout(request);
			break;
		case LIST_USER :
			response = UserService.getUserInfo(request);
			break;
		default :
			throw new RemoteException("알 수 없는 requestType입니다.");
		}

		return response;
	}
}
